package com.riku.security.service.impl;

import com.riku.security.mapper.SeCommonMapper;
import com.riku.security.mapper.SeServiceMapper;
import com.riku.security.model.*;
import com.riku.security.service.SeCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 该接口获取个人权限
 * Created by 1 on 2021/5/23.
 */
@Service
public class SeCommonServiceImpl implements SeCommonService{

    @Autowired
    private SeCommonMapper seCommonMapper;

    @Autowired
    private SeServiceMapper seServiceMapper;

    @Override
    public List<SeService> selectService(String serviceName,String userId) {
        return seServiceMapper.selectServiceByUserId(serviceName,userId);
    }

    @Override
    public List<SeSecurityMenu> selectMenuByUserId(String serviceCode, String userId, Integer includeApi ,Integer tree) {
        List<SeSecurityMenu> retList = new ArrayList<>();
        SeSecurityQuery query = new SeSecurityQuery();
        if(serviceCode != null){
            query.setServiceCodes(serviceCode.split(","));
        }

        query.setUserId(userId);
        query.setIncludeApi(includeApi);

        List<SeSecurityMenu> list = seCommonMapper.selectMenuByUserId(query);
        retList = list;
        if (Objects.equals(tree,1)) {
            retList = getTree(list,null);
        }

        return retList;
    }

    private List<SeSecurityMenu> getTree(List<SeSecurityMenu> list, Long parentId) {
        List<SeSecurityMenu> result = list
                .stream()
                .filter(node -> null == parentId ? parentId == node.getParentId() : parentId.equals(node.getParentId()))
                .map(node -> {
                    node.setChildList(getTree(list, node.getId()));
                    return node;
                })
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public List<SeSecurityApi> selectApiByUserId(String serviceCode, String userId) {
        SeSecurityQuery query = new SeSecurityQuery();
        query.setServiceCode(serviceCode);
        query.setUserId(userId);
        return seCommonMapper.selectApiByUserId(query);
    }

    /**
     * 登录时调用，根据服务code返回所有接口路径，请自行将返回值缓存
     * @param userId
     * @param serviceCodes
     * @return
     */
    @Override
    public List<Long> getApiSecurityForLogin(String userId, String ...serviceCodes) {
        SeSecurityQuery query = new SeSecurityQuery();
        query.setServiceCodes(serviceCodes);
        query.setUserId(userId);
        List<Long> list = seCommonMapper.selectRoleByUserIdMultiCodes(query);

        return list;
    }

    /**
     * 获取服务的所有API
     *
     * @param serviceCode
     * @return
     */
    @Override
    public List<SeApiRole> getApiAndRole(String serviceCode) {
        return seCommonMapper.getApiAndRole(serviceCode);
    }

    /**
     * 登录时调用，根据角色id返回所有接口路径，请自行将返回值缓存
     * @param userId
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> getApiSecurityForLogin(String userId, Long... roleIds) {
        SeRoleApiQuery query = new SeRoleApiQuery();
        query.setUserId(userId);
        query.setRoleIds(roleIds);
        List<SeSecurityApi> list = seCommonMapper.selectApiByUserIdMultiRoles(query);
        Set<String> ret = list.stream().map(SeSecurityApi::getApiPath).collect(Collectors.toSet());
        return ret;
    }
}
